﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ShopBL
    {
        public void Insert(tbl_Shop shop)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Shop newShop = new tbl_Shop
                    {
                        ShopNo=shop.ShopNo,
                        ShopName=shop.ShopName,
                        ShopLink=shop.ShopLink,
                        ShopCode=shop.ShopCode,
                        Img=shop.Img,
                        CreatedUser=shop.CreatedUser,
                        CreatedDate=shop.CreatedDate
                    };
                    contextdb.tbl_Shops.InsertOnSubmit(newShop);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_Shop shop)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateShop = contextdb.tbl_Shops.SingleOrDefault(u => u.ShopNo==shop.ShopNo);
                try
                {
                    if (updateShop != null)
                    {
                        updateShop.ShopNo=shop.ShopNo;
                        updateShop.ShopName=shop.ShopName;
                        updateShop.ShopLink=shop.ShopLink;
                        updateShop.ShopCode=shop.ShopCode;
                        updateShop.Img=shop.Img;
                        updateShop.CreatedUser = shop.CreatedUser;
                        updateShop.CreatedDate=shop.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int ShopNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteShop = contextdb.tbl_Shops.SingleOrDefault(u => u.ShopNo==ShopNo);
                try
                {
                    if (deleteShop != null)
                    {
                        contextdb.tbl_Shops.DeleteOnSubmit(deleteShop);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
